package com.example.inter.tourdemo.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.inter.tourdemo.R;
import com.example.inter.tourdemo.adapter.ClassifyAdapterLeft;
import com.example.inter.tourdemo.adapter.ClassifyAdapterRight;
import com.example.inter.tourdemo.bean.ClassifyLeftBean;
import com.example.inter.tourdemo.bean.ClassifyRightBean;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * Created by Inter巩海亮 on 2017-03-28.
 * 微信:
 * QQ号:
 * 邮箱:dahaizhiliang@126.com
 * 作用:地方特产
 */

public class SpecialtyFragment extends SupportFragment {

    @Bind(R.id.rv_left)
    RecyclerView mRvLeft;
    @Bind(R.id.rv_right)
    RecyclerView mRvRight;

    //数据
    ArrayList mClassifyLeftList;
    ArrayList mClassifyRightList;

    //商品左列表 文字
    private static String[] classify_name = new String[]{"推荐分类", "类别一", "类别二","类别三"
            , "类别四", "类别五", "类别六", "类别七"};
    //商品右列表
    private int[] classify_icons = new int[]{R.drawable.banner_a, R.drawable.banner_b, R.drawable.banner_c, R.drawable.banner_a
            ,R.drawable.banner_b, R.drawable.banner_c, R.drawable.banner_a, R.drawable.banner_b};
    private static String[] title_name = new String[]{"黑枸杞", "红枸杞", "纸皮核桃","巴旦木"
            , "木耳", "人参", "榛子", "蜂蜜"};

    private ClassifyAdapterLeft mClassifyAdapterLeft;
    private ClassifyAdapterRight mClassifyAdapterRight;

    public static SpecialtyFragment newInstance() {

        Bundle args = new Bundle();

        SpecialtyFragment fragment = new SpecialtyFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View classifyView = inflater.inflate(R.layout.fragment_cart, container, false);
        ButterKnife.bind(this, classifyView);

        initData();
        return classifyView;
    }


    public void initData() {
        mClassifyLeftList = new ArrayList();
        mClassifyRightList = new ArrayList();

        // TODO: 测试二级联动列表数据
        for (int i = 0; i < classify_name.length; i++) {
            ClassifyLeftBean leftBean = new ClassifyLeftBean();
            leftBean.classifyLeftName = classify_name[i];
            mClassifyLeftList.add(leftBean);
        }

        for (int j = 0; j < title_name.length; j++) {
            ClassifyRightBean rightBean = new ClassifyRightBean();
            rightBean.icon = classify_icons[j];
            rightBean.title = title_name[j];
            mClassifyRightList.add(rightBean);
        }

        //初始化Adapter
        initAdapter();
    }

    /**
     * 初始化adapter
     */
    private void initAdapter() {

        mClassifyAdapterLeft = new ClassifyAdapterLeft(R.layout.classify_item_left, mClassifyLeftList);
        LinearLayoutManager linearLayoutManagerLeft = new LinearLayoutManager(_mActivity);
        mRvLeft.setLayoutManager(linearLayoutManagerLeft);
        mRvLeft.setAdapter(mClassifyAdapterLeft);
        mRvLeft.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getContext(),"点击了: "+ mClassifyLeftList.get(position),Toast.LENGTH_SHORT).show();
            }
        });

        mClassifyAdapterRight = new ClassifyAdapterRight(R.layout.classify_item_right, mClassifyRightList);
        LinearLayoutManager linearLayoutManagerRight = new LinearLayoutManager(getContext());
        mRvRight.setLayoutManager(linearLayoutManagerRight);
        mRvRight.setAdapter(mClassifyAdapterRight);
        mRvRight.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

//                Intent intent = new Intent(mActivity, WaresDetailsActivity.class);
//                mActivity.startActivity(intent);

                Toast.makeText(getContext(),"点击了: "+ mClassifyRightList.get(position),Toast.LENGTH_SHORT).show();

            }
        });

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
